package com.example.sqliteoperate;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void clickCreateDB(View view){
        Log.v("life","clickCreateDB");
        MySqlite mySqlite = new MySqlite(this);
        SQLiteDatabase sqLiteDatabase = mySqlite.getWritableDatabase();
    }
    public void clickInsertDB(View view){
        Log.v("life","clickInsertDB");
        MySqlite mySqlite = new MySqlite(this);
        SQLiteDatabase sqLiteDatabase = mySqlite.getWritableDatabase();

        ContentValues values = new ContentValues();
        String name="bnbn";
        int price = 200;
        values.put("name",name);
        values.put("price",price);
        long id = sqLiteDatabase.insert("information",null,values);
        sqLiteDatabase.close();
    }

    public void clickQueryDB(View view){
        Log.v("life","clickQueryDB");
        MySqlite mySqlite = new MySqlite(this);
        SQLiteDatabase sqLiteDatabase = mySqlite.getReadableDatabase();

        Cursor cursor = sqLiteDatabase.query(MySqlite.getTableName(),
                null,null,null,
                null,null,null);

        while (cursor.moveToNext()){
            int id = cursor.getInt(0);
            String name = cursor.getString(cursor.getColumnIndex("name"));
            int price = cursor.getInt(2);
            Log.v("life","ID:"+id+",name:"+name+",price:"+price);
        }
        sqLiteDatabase.close();

    }

    public void clickDeleteDB(View view){
        MySqlite mySqlite = new MySqlite(this);
        SQLiteDatabase sqLiteDatabase = mySqlite.getWritableDatabase();
        sqLiteDatabase.execSQL("delete from information where name = 'bnbn'");
        sqLiteDatabase.close();
    }
}
